﻿using ScFramework.Core;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
/*
public class TestLocalServer : SubmoduleBase
{
    LocalServerEntityDataArgs _receiveArgs = new LocalServerEntityDataArgs();
    LocalServerEntityDataArgs _sendArgs = new LocalServerEntityDataArgs();

    readonly string path = "../NetTest/data.txt";
    public override void OnInit()
    {
        base.OnInit();
        GetSystemModule<MessageManager>().Get<LocalServerEntityDataEvent>().RegisterHandler(LocalServerEntityDataEvent.PhysicsProcess_RequestMessage, request);
        GetSystemModule<MessageManager>().Get<LocalServerEntityDataEvent>().RegisterHandler(LocalServerEntityDataEvent.PhysicsProcess_Send, send);

    }
    EventArgs request(object sender,EventArgs args)
    {
        LocalServerEntityDataArgs ans = new LocalServerEntityDataArgs();
        ans.messages.AddRange(_receiveArgs.messages);
        _receiveArgs.messages.Clear();
        return ans;
    }
    EventArgs send(object sender, EventArgs args)
    {
        _sendArgs.messages.AddRange((args as LocalServerEntityDataArgs).messages);
        return null;
    }
    public override void FixedUpdate()
    {
        base.FixedUpdate();

    //    Debug.Log(_receiveArgs + " " + _sendArgs);
        if (GetSystemModule<UserManager>().IsHost)
        {
            write();
        }
        else
        {
            read();
        }
    }
    void write()
    {
        if (_sendArgs.messages.Count>0)
        {
            if (File.Exists(path))
            {
                return;
            }
            else
            {
                JsonUtil.SerializeToFile(Convert(_sendArgs.messages),path);
                _sendArgs.messages.Clear();
            }
        }
    }
    void read()
    {
        if (File.Exists(path))
        {
            try
            {
                string js = File.ReadAllText(path);
                _receiveArgs.messages.AddRange(Convert(JsonUtil.Deserialize<TestSerializableMsgs>(js)));
                File.Delete(path);
            }catch(Exception e)
            {
                Debug.LogWarning(e);
            }

        }
    }
    List<EntityDataMsg>Convert(TestSerializableMsgs msgs)
    {
        var ans = new List<EntityDataMsg>();
        foreach(var x in msgs.testMsgs)
        {
            var tp = new EntityDataMsg
            {
                Type = x.messageType,

            };
            if (x.messageType == EntityDataMsgType.Sync)
            {
                tp.Args = new EntityChangeArgs
                {
                    EntityID = x.entityID.left(),
                    ValueID = x.entityID.right().combine(x.valueInfo.ID),
                    Value = x.valueInfo.ValuePair.Value
                };
            } else tp.Args = x.entityID;
       //     Args = ((x.messageType == PhysicsMessageType.Sync) ?  : x.entityID)
            ans.Add(tp);
        }
        return ans;
    }
    TestSerializableMsgs Convert(List<EntityDataMsg> args)
    {
        var ans = new TestSerializableMsgs();
        foreach(var x in args)
        {
            EntityChangeArgs tp = x.Args as EntityChangeArgs;
            PrefabValuePair value = null;
            if (x.Type == EntityDataMsgType.Sync)
            {
                value = new PrefabValuePair
                {
                    ID = tp.ValueID.right(),
                    ValuePair = new TestSerializableValue(tp.Value)
                };
            }
            ans.testMsgs.Add(new TestMsg
            {
                messageType = x.Type,
                entityID = (x.Type == EntityDataMsgType.Sync) ? tp.EntityID.combine(tp.ValueID.left()) : ((UInt32)x.Args),
                valueInfo = value,



            });
        }
        return ans;
    }
}
[Serializable]
public class TestSerializableMsgs
{
    public List<TestMsg> testMsgs=new List<TestMsg>();
}
[Serializable]
public class TestMsg
{
    public EntityDataMsgType messageType;
    public UInt32 entityID;
    public PrefabValuePair valueInfo;

}*/